package com.ad.pet.app.account.order.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ad.pet.MainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.account.adapter.OrderTypeAdapter;
import com.ad.pet.app.account.share.ShareDialog;
import com.ad.pet.app.shopcar.confirm.ConfirmOrderContracts;
import com.ad.pet.app.shopcar.confirm.ConfirmOrderPresenter;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.fragment.LoadingFragment;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.account.Banlance;
import com.ad.pet.models.remote.account.UserBuyReturn;
import com.ad.pet.models.remote.account.order.BigOrder;
import com.ad.pet.models.remote.account.order.OrderType;
import com.ad.pet.models.remote.account.order.PayCompleted;
import com.ad.pet.models.remote.account.order.SignCheck;
import com.ad.pet.models.remote.cart.BigShipingw;
import com.ad.pet.models.remote.cart.Coupons;
import com.ad.pet.models.remote.mall.Counpous;
import com.ad.pet.ui.SimpleDividerDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 13620953972
 * Created by SEELE on 2019/2/15.
 */

public class InDoorServiceFragment extends LoadingFragment implements OrderStatusContract.View, ConfirmOrderContracts.View {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    @Inject
    OrderStatusPresenter mStatusPresenter;

    private String mStatus;
    private OrderTypeAdapter mOrderTypeAdapter;

    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mSwipeRefreshLayout;

//    private ShareBoardConfig mBoardConfig;

    private int page = 1;

//    @BindView(R.id.tv_tips)
//    TextView mTvTips;
    private static final int RECHARGE_REPOSED_CODE = 289;
    @Inject
    ConfirmOrderPresenter mConfirmOrderPresenter;
    private SignCheck mSignCode;
    private ShareDialog mShareDialog;
    //    @Inject
//    PacketInfoPresenter mPacketInfoPresenter;


    @Override
    protected int contentLayout() {
        return R.layout.fragment_order;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initData() {
        mConfirmOrderPresenter.onCreate(this);
        mConfirmOrderPresenter.shareH5Url();
    }

    @Override
    protected void initEvents() {


        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mOrderTypeAdapter.setEnableLoadMore(false);
                mSwipeRefreshLayout.setRefreshing(false);
                mStatusPresenter.getOrderStatus(mStatus, String.valueOf(page));

            }
        });

        mOrderTypeAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mStatusPresenter.getOrderStatus(mStatus, String.valueOf(++page));
            }
        }, mRecyclerView);


    }

    @Override
    protected void initViews() {
        mStatusPresenter.onCreate(this);
        mStatus = getArguments().getString(Constant.STATUS);
//        mPacketInfoPresenter.onCreate(this);
        mStatusLayoutManager.showContent();

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mOrderTypeAdapter = new OrderTypeAdapter(null);
        mRecyclerView.addItemDecoration(new SimpleDividerDecoration(getActivity(), 9, R.color.bg_color));
        mRecyclerView.setAdapter(mOrderTypeAdapter);
        mOrderTypeAdapter.bindToRecyclerView(mRecyclerView);
        mOrderTypeAdapter.setEmptyView(R.layout.layout_empty_address);
        ((TextView)mOrderTypeAdapter.getEmptyView().findViewById(R.id.tv_title)).setText("您还没有相关服务哦");
//        if (TextUtils.equals("paid", mStatus)) {
//            mTvTips.setVisibility(View.GONE);
//        } else {
//            mTvTips.setVisibility(View.VISIBLE);
//        }


//        mStatusPresenter.getOrderStatus(mStatus, String.valueOf(page));
        mShareDialog = new ShareDialog(getActivity());
//        mBoardConfig = new ShareBoardConfig();
//        mBoardConfig.setIndicatorVisibility(false);
//        mBoardConfig.setTitleText(getString(R.string.share));
//        mBoardConfig.setCancelButtonText(getString(R.string.cancel));
    }

    @Override
    public void initInjector() {
        getComponent(MainActivityCompent.class).inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    public static InDoorServiceFragment newInstance(String status) {
        Bundle args = new Bundle();
        InDoorServiceFragment fragment = new InDoorServiceFragment();
        args.putString(Constant.STATUS, status);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {
//        mStatusLayoutManager.showEmpty();
    }

    @Override
    public void error(int code, String msg) {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(false);
        }

    }



    @Override
    public void getPendingSuccess(List<BigOrder> bigOrder) {

    }

    @Override
    public void getPacketInfoSuccess(BigOrder bigOrder) {

    }

    @Override
    public void getOrderStatusSuccess(List<BigOrder> orderStatuses) {


//        mStatusLayoutManager.showContent();
//        mSwipeRefreshLayout.setRefreshing(false);
//        if (page == 1) {
//            mOrderTypeAdapter.setNewData(orderStatuses);
//        } else {
//            mOrderTypeAdapter.addData(orderStatuses.getOrder_list());
//            mOrderTypeAdapter.loadMoreComplete();
//        }
//        if (orderStatuses.getOrder_list().size() < 10) {//CommonConstant.PAGE_COUNT
//            //暂无更多数据
//            mOrderTypeAdapter.loadMoreEnd();
//        }
//        if (orderStatuses.getOrder_list().size() == 0 && page == 1) {
//            mOrderTypeAdapter.setEmptyView(R.layout.layout_empty_order);
////            mTvTips.setVisibility(View.GONE);
//            mOrderTypeAdapter.getEmptyView().findViewById(R.id.tv_go_shop).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent();
//                    intent.putExtra("goHome",1);
//                    getActivity().setResult(Activity.RESULT_OK,intent);
//                    getActivity().finish();
//                }
//            });
//        }else {
////            mTvTips.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public void cancelOrderSuccess() {

    }

    @Override
    public void confirmOrderSuccess() {

    }

    @Override
    public void getGoodOrderDetailSuccess(BigOrder bigOrder) {

    }

    @Override
    public void myAppointListSuccess(List<OrderType> list) {

    }

    @Override
    public void cancelAppointListSuccess() {

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode== MainActivity.REQUEST_GO_MALL&&resultCode== Activity.RESULT_OK){
//            page=1;
//            mStatusPresenter.getOrderStatus(mStatus, String.valueOf(page));
//                getActivity().finish();
//        }
    }



    @Override
    public void getCouponsSuccess(List<Counpous> coupons) {

    }

    @Override
    public void checkCouponsSuccess(Coupons coupons) {

    }

    @Override
    public void confirmOrderSuccess(PayCompleted shop) {

    }

    @Override
    public void getUserBanceSuccess(Banlance banlance) {

    }

    @Override
    public void getShipingWaysSuccess(BigShipingw shippingWay) {

    }

    @Override
    public void paymentBalanceSuccess(int toPay) {

    }

    @Override
    public void checkIsSignSuccess(SignCheck signCheck) {

    }

    @Override
    public void daySignSuccess(SignCheck signCheck) {

    }

    @Override
    public void sharH5UrlSuccess(SignCheck signCheck) {
        mSignCode = signCheck;
    }

    @Override
    public void get_user_buy_return_success(UserBuyReturn userBuyReturn) {

    }

    private void initUMshare() {
        if (mSignCode==null){
            return;
        }
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_share_img);

        mShareDialog.share(mSignCode.getUrl(),getString(R.string.share_title),bitmap,getString(R.string.share_desc));
        mShareDialog.show();
//        UMWeb mUMWeb = new UMWeb(mSignCode.getUrl());
//        UMImage mUmImage = new UMImage(getActivity(), R.drawable.ic_share_img);
//        mUMWeb.setThumb(mUmImage);
//        mUMWeb.setTitle(getString(R.string.share_title));
//        mUMWeb.setDescription(getString(R.string.share_desc));
//        new ShareAction(getActivity())
//                .withMedia(mUMWeb)
//                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.QQ,SHARE_MEDIA.FACEBOOK)
//                .setCallback(new UMShareLinstener()).open(mBoardConfig);
    }
}
